package com.dish.action;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.User;

import com.dish.bean.Dish;
import com.dish.bean.MyUsers;
import com.dish.biz.DishService;
import com.dish.biz.UserService;
import com.dish.dao.UserDAO;
import com.dish.utils.DAOFactory;
import com.dish.utils.PageModel;
import com.dish.utils.SafetyControl;

/**
 * Servlet implementation class UserRegister
 */
@WebServlet("/UserRegister")
public class UserRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserRegister() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    private void registUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
    {
    	HttpSession session = request.getSession(true);
    	String userName = request.getParameter("registName1");
    	String userPass = request.getParameter("registPass");
    	String registPhone = request.getParameter("registPhone");
    	String registCheck = request.getParameter("registCheck");
    	String randCheck = (String) session.getAttribute("randNum");
    	MyUsers registerUser = new MyUsers();
    	//SafetyControl sc = new SafetyControl();
    	registerUser.setUsername(userName);
    	registerUser.setPassword(userPass);
    	registerUser.setPhonenumber(registPhone);
    	//registerUser.setPassword(sc.MD5(userPass));
    	UserService userserv = new UserService();
		RequestDispatcher rd = null;
		if(registCheck.equals(randCheck)){
			if (!userserv.validateUser(registerUser)) {  //当前数据库没有该用户时
				session.setAttribute("registUser", registerUser);
				UserDAO userdao = (UserDAO) DAOFactory.newInstance("UserDAO");
				userdao.insertUser(registerUser);
				
				response.sendRedirect("index.jsp");
			} else {
				// 否则，重新注册
				response.sendRedirect("registUser.jsp");
			}
		}
		else {
			response.sendRedirect("registUser.jsp");
		}
    	
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
//		RequestDispatcher requestDispatcher = request.getRequestDispatcher("registUser.html");
//		requestDispatcher.forward(request, response);
		response.setContentType("text/html;charset=utf-8");
		registUser(request, response);
//		String actiontype = request.getParameter("actiontype");
//		switch (actiontype) {
//		case "regist":
//			// 登录
//			
//			break;
//		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
